package com.ly.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.reggie.common.R;
import com.ly.reggie.dto.SetmealDto;
import com.ly.reggie.mapper.SetMealMapper;
import com.ly.reggie.pojo.Setmeal;
import com.ly.reggie.pojo.SetmealDish;
import com.ly.reggie.service.SetMealService;
import com.ly.reggie.service.SetmealDishService;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, Setmeal> implements SetMealService {

    @Autowired
    SetMealService setMealService;

    @Autowired
    SetmealDishService setmealDishService;

    /**
     * 新增套餐，同时插入菜品对应的口味，需要操作两张表：Setmeal，SetmealDish
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐本身数据
        this.save(setmealDto);

        //获得套餐菜品数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //处理SetmealId
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存获得的菜品数据
        setmealDishService.saveBatch(setmealDishes);
    }


    /**
     * 将套餐信息展示在修改页面
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public SetmealDto showById(Long id) {
        //根据id获取setmeal
        Setmeal setmeal = setMealService.getById(id);
        //创建一个SetmealDish
        SetmealDto setmealDto = new SetmealDto();
        //将setmeal复制给setmealDish
        if (setmeal != null) {
            BeanUtils.copyProperties(setmeal, setmealDto);
        }


        //构造条件查询器
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件
        lambdaQueryWrapper.eq(id != null, SetmealDish::getSetmealId, id);
        //查询出菜品集合
        List<SetmealDish> list = setmealDishService.list(lambdaQueryWrapper);

        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }


    /**
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //
        this.updateById(setmealDto);

        //删除旧套餐菜品
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getDishId, setmealDto.getId());
        setmealDishService.remove(lambdaQueryWrapper);

        //增加新的菜品
        Long id = setmealDto.getId();
        //获取前端传来的新的菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //更新前端获取到的菜品id
        setmealDishes.stream().map((item) ->{
            item.setSetmealId(id);

            return item;
        }).collect(Collectors.toList());

        //保存前端获取的菜品
        setmealDishService.saveBatch(setmealDishes);

    }


}
